<?php
/**
 * 栏目挂件
 * @author liuyemin
 *
 */
class CategoryWidget extends CWidget{
	
	public $cid = 0;	//0,获取所有
	public $row = 14;	//默认，获取所有栏目
	public $class = '';	//默认li下面的样式
	
	/**
	 * 属性初始化,如果外边存在设置，将覆盖此处
	 * @see CWidget::init()
	 */
	public function init(){

	}
	
	public function run(){
		//获取栏目列表
		$this->getCateList();
	}
	
	protected function getCateList(){
		$where = $this->cid == 0 ? '' : 'where category_id = ' . $this->cid;
		$sql = "SELECT name,title FROM {{category}} {$where} limit 0,{$this->row}";
		$list = Yii::app()->db->createCommand($sql)->queryAll();
		if(is_array($list)){
			foreach($list as $v){
					echo '<li class="'.$this->class.'"><a href="'.Yii::app()->controller->createUrl('category/index',array('name'=>$v['name'])).'">'.$v['title'].'</a></li>';
			}	
		}
	}
}